package com.dd.ma;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

//发送post
public class PostAction{
	private String actionUrl = "";
	private String cookie = "";
	private String agent = "";
	private static String AcceptEncoding = "gzip";
	private static String Host = "game1-cbt.ma.sdo.com:10001";
	private static String ContentType = "application/x-www-form-urlencoded";
	private static String AcceptLanguage = "zh-cn";
	
	public String getAcceptEncoding() {
		return AcceptEncoding;
	}

	public void setAcceptEncoding(String acceptEncoding) {
		AcceptEncoding = acceptEncoding;
	}

	public String getHost() {
		return Host;
	}

	public void setHost(String host) {
		Host = host;
	}

	public String getContentType() {
		return ContentType;
	}

	public void setContentType(String contentType) {
		ContentType = contentType;
	}

	public String getAcceptLanguage() {
		return AcceptLanguage;
	}

	public void setAcceptLanguage(String acceptLanguage) {
		AcceptLanguage = acceptLanguage;
	}
	public  PostAction(){
		
	}
	public  PostAction(String url,String cookie,String agent){
		this.actionUrl=url;
		this.cookie=cookie;
		this.agent=agent;
	}
	public String getActionUrl() {
		return actionUrl;
	}
	public void setActionUrl(String actionUrl) {
		this.actionUrl = actionUrl;
	}
	
	public String getCookie() {
		return cookie;
	}
	public void setCookie(String cookie) {
		this.cookie = cookie;
	}
	public String getAgent() {
		return agent;
	}
	public void setAgent(String agent) {
		this.agent = agent;
	}
	public String getActionResponse(NameValuePair[] data) throws ServerErrorException,ServerDownException {
		String response = "";
		try {
			HttpClient httpClient = new HttpClient();
			PostMethod postMethod = new PostMethod(actionUrl);
			postMethod.setRequestHeader("Accept-Encoding", this.getAcceptEncoding());
			postMethod.setRequestHeader("Host", this.getHost());
			postMethod.setRequestHeader("Content-Type",this.getContentType());
			postMethod.setRequestHeader("Cookie",cookie);
			postMethod.setRequestHeader("Accept-Language", this.getAcceptLanguage());
			postMethod.setRequestHeader("User-Agent",agent);
			postMethod.setRequestBody(data);
			httpClient.executeMethod(postMethod);
			response = MaUtil.getSndaResponseString(postMethod.getResponseBodyAsStream());
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		if(response.indexOf("<code>9000</code>")!=-1){
			throw new  ServerErrorException("code 9000 error:网络链接超时,回到首页");
		}
		if(response.indexOf("服务器正在维护")!=-1){
			throw new  ServerDownException("code 1020 error:服务器正在维护");
		}
		return response;
	}

}
